package com.yfp.goods.controller;

import cn.hutool.core.bean.BeanUtil;
import com.yfp.common.core.domain.R;
import com.yfp.common.core.utils.poi.ExcelUtil;
import com.yfp.common.core.web.controller.BaseController;
import com.yfp.common.core.web.page.TableDataInfo;
import com.yfp.common.log.annotation.Log;
import com.yfp.common.log.enums.BusinessType;
import com.yfp.goods.domain.Combo;
import com.yfp.goods.domain.dto.ComboDto;
import com.yfp.goods.domain.dto.ComboGroupDTO;
import com.yfp.goods.domain.vo.ComboExportVO;
import com.yfp.goods.domain.vo.ComboVo;
import com.yfp.goods.service.ComboService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * 套餐表(Combo)表控制层
 *
 *
 * @since 2023-12-27 13:33:24
 */
@RestController
@RequestMapping("combo")
@Tag(name = "套餐管理")
public class ComboController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private ComboService comboService;

    /**
     * 分页查询
     *
     * @param combo 筛选条件
     * @return 查询结果
     */
    @GetMapping
    @Operation(summary = "分页查询")
    public TableDataInfo queryByPage(Combo combo, @Valid Integer pageNum, @Valid Integer pageSize) {
        return getDatableByStream(this.comboService.queryAll(combo), pageNum, pageSize);
    }

    @PostMapping("/export")
    @Operation(summary = "套餐信息导出")
    public void export(HttpServletResponse response, Combo combo) {
        List<ComboDto> comboDtos = comboService.queryAll(combo);
        List<ComboExportVO> comboExportVOS = comboDtos.stream().map(comboDto -> {
            ComboExportVO comboExportVO = BeanUtil.toBean(comboDto, ComboExportVO.class);
            Optional.ofNullable(comboDto.getTagName()).ifPresent(tagNames -> {
                comboExportVO.setTagName(tagNames.stream().collect(Collectors.joining(",")));
            });
            return comboExportVO;
        }).toList();

        ExcelUtil<ComboExportVO> util = new ExcelUtil<>(ComboExportVO.class);
        util.exportExcel(response, comboExportVOS, "套餐信息");
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @Operation(summary = "通过主键查询单条数据")
    public R<Combo> queryById(@PathVariable("id") String id) {
        return R.ok(this.comboService.queryById(id));
    }


    /**
     * 通过主键查询多条数据
     *
     * @return 单条数据
     */
    @Operation(summary = "通过主键查询多条数据")
    @GetMapping("list/ids")
    public R<List<Combo>> queryByIds(@RequestParam List<String> comboIds) {
        return R.ok(this.comboService.queryByIds(comboIds));
    }

    /**
     * 新增套餐
     *
     * @param comboVo 实体
     * @return 新增结果
     */
    @PostMapping
    @Operation(summary = "新增套餐")
    @Log(title = "套餐管理", businessType = BusinessType.INSERT)
    public R add(@RequestBody ComboVo comboVo) {
        return this.comboService.addCombo(comboVo);
    }

    /**
     * 编辑数据
     *
     * @param comboVo 实体
     * @return 编辑结果
     */
    @PutMapping
    @Operation(summary = "编辑数据")
    @Log(title = "套餐管理", businessType = BusinessType.UPDATE)
    public R<ComboVo> edit(@RequestBody ComboVo comboVo) {
        return R.ok(this.comboService.updateComboVo(comboVo));
    }


    @GetMapping("shelf/{comboId}")
    @Operation(summary = "上下架套餐")
    @Log(title = "上下架套餐", businessType = BusinessType.UPDATE)
    public R shelfCombo(@PathVariable("comboId") String comboId) {
        return this.comboService.shelfCombo(comboId) ? R.ok() : R.fail();
    }

    @GetMapping("groupShelf/{comboId}")
    @Operation(summary = "上下架团餐")
    @Log(title = "上下架团餐", businessType = BusinessType.UPDATE)
    public R groupShelfCombo(@PathVariable("comboId") String comboId) {
        return this.comboService.groupShelfCombo(comboId) ? R.ok() : R.fail();
    }


    @GetMapping("plan/{comboId}")
    @Operation(summary = "通过套餐id查询所在投放计划")
    public R queryComboPlan(@PathVariable("comboId") String comboId) {
        return R.ok(this.comboService.queryComboPlan(comboId));
    }

    @GetMapping("/list")
    @Operation(summary = "获取套餐列表")
    @Log(title = "获取套餐列表")
    public R<List<Combo>> getComboIds(String cityName)
    {
        Combo combo = new Combo();
        combo.setCityName(cityName);
        return R.ok(this.comboService.getComboIds(combo));
    }

    @GetMapping("/groupShelf/list")
    @Operation(summary = "获取上架团餐列表")
    public R<List<ComboGroupDTO>> getGroupShelfComboList() {
        return R.ok(this.comboService.getGroupShelfComboList());
    }
}

